<?php

namespace Mobile\Controller;

/**
 * 房型房间移动端接口
 * @modify trainyao 2016-01-23 创建
 */
class RoomController extends ParentController {

    protected function _initialize() {
        parent::_initialize();
        $this->validateUtil = new \Org\Util\Validate();
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'hotel' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $userId = $param['userId'];

        //检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId, $userId);

        //检查客栈是否在有效期内
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelLogic->checkHotelActive($hotelId);
        $hotelLogic->checkHotelStatus($hotelId);
    }

    /**
     * 获取房间数据
     * @modify trainyao 2016-01-23    创建
     * @modify trainyao 2016-03-17 代码测试友好
     * @for ios   v1.0.0
     */
    public function items() {
        $error = false;
        $param = I('post.');

        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'hotel' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
        );
        $validateResult = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validateResult) {
            $error = true;
        }

        if (!$error) {
            $userId = $param['userId'];
            $token = $param['token'];
            $this->checkMobileToken($userId, $token);

            // 获取房间数据
            $hotelId = $param['hotel'];
            $roomTypeLogic = A('Common/RoomType', 'Logic');
            $roomTypeInfo = $roomTypeLogic->getInfoByHotel($hotelId);
            $roomCount = 0;

            // 获取房间权限
            $needCheckRoomRight = false;
            $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
            if (!$adminAuthorityLogic->checkIsAdmin($userId, $hotelId)) {
                $needCheckRoomRight = true;
                $accountLogic = A('Common/Account', 'Logic');
                $roomAuthority = $accountLogic->getAdminRoomAuthority($userId, $hotelId);
            }

            $version = getVersion(array('2.0.17'));
            if ($version >= '2.0.17') {
                $roomTypeModel = D('RoomType');
                $roomTypes = $roomTypeModel->getInfoByHotel($hotelId, false);
                $roomServiceList = array();
                if (!empty($roomTypes)) {
                    $roomTypeIds = array_column($roomTypes, 'id');
                    // 维修房数据
                    $roomServiceModel = D('RoomService');
                    $roomServiceList = $roomServiceModel->getServiceList($roomTypeIds, null, null, NOT_DELETE, true);
                    $roomServiceList = getArrayValueAsIndex($roomServiceList, 'roominfo_id');
                }
            }

            // 处理返回的房型数据
            foreach ($roomTypeInfo as $roomTypeIndex => &$roomType) {
                foreach ($roomType['room'] as $index => &$roomItem) {
                    // 去除没有权限的房间
                    if (!$needCheckRoomRight || in_array($roomItem['id'], $roomAuthority)) {
                        $roomItem = array(
                            'roomId' => $roomItem['id'],
                            'name' => $roomItem['name'],
                            'from' => $roomItem['roomfrom'],
                            'dirty' => $roomItem['isdirty'],
                            'ishourroom' => $roomItem['ishourroom'],
                            'islongroom' => $roomItem['islongroom'],
                        );
                        if ($version >= '2.0.17') {
                            // 是否维修房
                            if (isset($roomServiceList[$roomItem['roomId']])) {
                                $roomItem['isService'] = (int)$roomServiceList[$roomItem['roomId']]['isservice'];
                                $roomItem['startDate'] = date('Y-m-d', $roomServiceList[$roomItem['roomId']]['startdate']);
                                $roomItem['endDate'] = date('Y-m-d', $roomServiceList[$roomItem['roomId']]['enddate']);
                            } else {
                                $roomItem['isService'] = ROOM_CLEAN;
                            }
                        }
                    } else {
                        unset($roomType['room'][$index]);
                    }
                }
                unset($roomItem);
                $roomType['room'] = array_values($roomType['room']);


                // 取出没有房间的房型
                if (empty($roomType['room'])) {
                    unset($roomTypeInfo[$roomTypeIndex]);
                    continue;
                } else {
                    $roomCount += count($roomType['room']);
                    $roomType = array(
                        'typeId' => $roomType['id'],
                        'name' => $roomType['name'],
                        'price' => $roomType['price'],
                        'room' => $roomType['room'],
                    );
                }
            }
            unset($roomType);

            returnJson(200, 200, array(
                'roomCount' => $roomCount,
                'rooms' => array_values($roomTypeInfo),
            ));
        }
    }

    /**
     * @description 设置脏房
     * @modify songanwei 2016-01-23
     * @for v1.0.0  ios
     */
    public function setDirty() {
        $error = false;
        $params = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'hotel' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'roomId' => 'notnull,notempty,id',
            'status' => 'notnull,notempty,roomDirty'
        );
        $validateResult = $this->ajaxValidateReturn($this->validateUtil, $params, $validateRule);
        if (!$validateResult) {
            $error = true;
        }

        if (!$error) {
            $userId = $params['userId'];
            $token = $params['token'];
            $hotelId = $params['hotel'];
            $roomId = $params['roomId'];
            $this->checkMobileToken($userId, $token);

            // 检查房间是否都可操作
            $roomLogic = A('Common/Room', 'Logic');
            $roomInfo = $roomLogic->getRoom($hotelId, $roomId);
            if (empty($roomInfo)) {
                returnJson(403, 202, 'error');
                $error = true;
            }

            if (!$error) {
                $status = $params['status'];
                $timeStamp = $roomLogic->setDirty($hotelId, $roomId, ($status == 'dirty' ? ROOM_DIRTY : ROOM_CLEAN));
                returnJson(200, 200, $timeStamp);
            }
        }
    }
    
    /**
     * 获取预设价格
     * @add Paddy 2016-12-1 创建
     */
    public function getPriceByHotelId(){
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'date' => 'notnull,notempty,isDate'
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            returnJson(403, 212, null);
        }
        $hotelId = $param['hotel'];
        $date = $param['date'];
        $dateTime = strtotime(date('Y-m-d', strtotime($date)));
        
        $startTime = $dateTime - 3 * 86400;
        $endTime = $dateTime + 33 * 86400;
        
        $roomLogic = A('Common/Room', 'Logic');
        $priceInfo = $roomLogic->getRoomPrice($hotelId, $startTime, $endTime);
        
        returnJson(200, 200, $priceInfo);
    }

    /**
     * 获取预设价格
     * @add zhangzhen 2017-5-26 创建
     */
    public function getRoomTypePrice(){
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'date' => 'notnull,notempty,isDate'
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            returnJson(403, 212, null);
        }
        $hotelId = $param['hotel'];
        $date = $param['date'];
        $dateTime = strtotime(date('Y-m-d', strtotime($date)));
        
        $startTime = $dateTime - 3 * 86400;
        $endTime = $dateTime + 33 * 86400;
        
        $roomLogic = A('Common/Room', 'Logic');
        $priceInfo = $roomLogic->getRoomTypePrice($hotelId, $startTime, $endTime);
        
        returnJson(200, 200, $priceInfo);
    }

    /**
     * 设置房间为维修房
     * @modify wenhui 2017-12-01 创建
     */
    public function setService() {
        $param = I('post.');

        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
            'roomTypeId' => 'notnull,notempty,id',
            'roomId' => 'notnull,notempty,id',
        );

        if (isset($param['startDate']) && isset($param['endDate'])) {
            $validateRule['startDate'] = 'notnull,notempty,isDate';
            $validateRule['endDate'] = 'notnull,notempty,isDate';
        }

        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $userId = $param['userId'];
        $token = $param['token'];
        $hotelId = $param['hotel'];
        $roomTypeId = $param['roomTypeId'];
        $roomId = $param['roomId'];
        $status = isset($param['status']) ? $param['status'] : 0;
        $startDate = isset($param['startDate']) ? $param['startDate'] : null;
        $endDate = isset($param['endDate']) ? $param['endDate'] : null;

        // 校验token
        $this->checkMobileToken($userId, $token);
        // 检查房间是否都可操作
        $roomLogic = A('Common/Room', 'Logic');
        $roomInfo = $roomLogic->getRoom($hotelId, $roomId);

        if (empty($roomInfo)) {
            returnJson(403, 202, 'error');
        }

        $setResult = $roomLogic->setService($hotelId, $roomTypeId, $roomId, $status, $startDate, $endDate);

        returnJson(200, 200);
    }
}
